﻿using System;
using System.Collections.Generic;
using System.Text;
using com.gargoylesoftware.htmlunit.html;
using WatiN.Core.Interfaces;
using WatiN.Core.Constraints;
using org.w3c.dom;

namespace WatiN.Core.HtmlUnit
{
	public class HUElementFinder : ElementFinderBase
	{
	
		public HUElementFinder(List<ElementTag> elementTags, IElementCollection elementCollection, DomContainer domContainer) : base(elementTags, elementCollection, domContainer) { }

        public HUElementFinder(List<ElementTag> elementTags, BaseConstraint constraint, IElementCollection elementCollection, DomContainer domContainer) : base(elementTags, constraint, elementCollection, domContainer) { }

		public HUElementFinder(string tagName, string inputType, IElementCollection elementCollection, DomContainer domContainer) : base(tagName, inputType, elementCollection, domContainer) {}

		public HUElementFinder(string tagName, string inputType, BaseConstraint constraint, IElementCollection elementCollection, DomContainer domContainer) : base(tagName, inputType, constraint, elementCollection, domContainer) { }


		
		protected override List<INativeElement> FindElements(BaseConstraint constraint, ElementTag elementTag, ElementAttributeBag attributeBag, bool returnAfterFirstMatch, IElementCollection elementCollection)
		{
			List<INativeElement> elements = new List<INativeElement>();

			if (elementCollection.Elements is HUElement)
				elements.Add(elementCollection.Elements as HUElement);
			else
				elements = elementCollection.Elements as List<INativeElement>;

			List<INativeElement> children = new List<INativeElement>();

			if (elements != null)
			{
				foreach (HUElement eon in elements)
				{
					HtmlElement he = eon.Object as HtmlElement;

					if (he != null)
					{
					
						if (FinishedAddingChildrenThatMetTheConstraints(eon, attributeBag, elementTag, constraint, children, returnAfterFirstMatch))
							return children;
					
					}
				}
			}

			return children;
		}

		protected override List<INativeElement> FindElementById(BaseConstraint constraint, ElementTag elementTag, ElementAttributeBag attributeBag, bool returnAfterFirstMatch, IElementCollection elementCollection)
		{
			List<INativeElement> children = new List<INativeElement>();

			var elements = elementCollection.Elements as List<INativeElement>;
		

			foreach (INativeElement eon in elements)
			{
				var element = eon as HUElement;

				if (element != null)
				{
					HtmlElement he = element.Object as HtmlElement;

					if (he != null)
					{
						if (he.getId().ToLowerInvariant().Equals(((AttributeConstraint)constraint).Value.ToLowerInvariant()) && he.getTagName().ToLowerInvariant().Equals(elementTag.TagName.ToLowerInvariant()))
						{
							if (FinishedAddingChildrenThatMetTheConstraints(eon, attributeBag, elementTag, constraint, children, returnAfterFirstMatch))
								return children;
						}
					}
				}
			}

			return children;
		}

		protected override void WaitUntilElementReadyStateIsComplete(INativeElement element)
		{
			
		}
	}
}
